package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;


  /*  // 构造器

    private SpecGroupService specGroupService;
    private SpecParamService specParamService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }*/
    /*
    * 根据分类id查询规格参数组
    * */
    @GetMapping("groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategoryId(@RequestParam("id") Long id){
        return ResponseEntity.ok(
                SpecGroupDTO.convertEntityList(
                        specGroupService.query().eq("category_id",id).list()));
    }

    /*
     * 根据规格参数组id 查询规格参数项
     * */

    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamBySpecGroupId(@RequestParam("id") Long id){
        return ResponseEntity.ok(
                SpecParamDTO.convertEntityList(
                        specParamService.query().eq("category_id",id).list()));


    }

    /*
    * 查询规格参数集合 方式2
    * */
    @GetMapping("params2")
    public ResponseEntity<List<SpecParamDTO>> querySpecParam(
            @RequestParam(value = "categoryId",required = false)Long categoryId,
            @RequestParam(value = "groupId",required = false)Long groupId,
            @RequestParam(value="searching" ,required = false)Boolean search
    ){
        return ResponseEntity.ok(specParamService.querySpecParam(categoryId,groupId,search));
    }
}
